<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!DOCTYPE html>
<html>

<head>
    <meta charset="UTF-8">
    <title>欢迎页面-X-admin2.0</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport"
          content="width=device-width,user-scalable=yes, minimum-scale=0.4, initial-scale=0.8,target-densitydpi=low-dpi"/>
    <link rel="shortcut icon" href="/favicon.ico" type="image/x-icon"/>
    <link rel="stylesheet" href="static/css/font.css">
    <link rel="stylesheet" href="static/css/xadmin.css">
    <script type="text/javascript" src="https://cdn.bootcss.com/jquery/3.2.1/jquery.min.js"></script>
    <script type="text/javascript" src="static/lib/layui/layui.js" charset="utf-8"></script>
    <script type="text/javascript" src="static/js/xadmin.js"></script>
    <link rel="stylesheet" href="ztree/css/demo.css" type="text/css">
    <link rel="stylesheet" href="ztree/css/zTreeStyle/zTreeStyle.css" type="text/css">
    <script type="text/javascript" src="ztree/js/jquery.ztree.core.js"></script>
    <script type="text/javascript" src="ztree/js/jquery.ztree.excheck.js"></script>

    <!-- 让IE8/9支持媒体查询，从而兼容栅格 -->
    <!--[if lt IE 9]>
    <script src="https://cdn.staticfile.org/html5shiv/r29/html5.min.js"></script>
    <script src="https://cdn.staticfile.org/respond.js/1.4.2/respond.min.js"></script>
    <![endif]-->
</head>

<body>
<div class="x-body">
    <form action="addRole" method="post" class="layui-form layui-form-pane">

        <!--保存当前用户勾选的权限id-->
        <input type="hidden" name="auths" value="">

        <div class="layui-form-item">
            <label for="name" class="layui-form-label">
                <span class="x-red">*</span>角色名
            </label>
            <div class="layui-input-inline">
                <input type="text" id="name" name="rname" required="" lay-verify="required"
                       autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item layui-form-text">
            <label class="layui-form-label">
                拥有权限
            </label>

            <div>

                <ul id="mytree" class="ztree"></ul>

            </div>

        </div>
        <div class="layui-form-item layui-form-text">
            <label for="desc" class="layui-form-label">
                描述
            </label>
            <div class="layui-input-block">
                <textarea placeholder="请输入内容" id="desc" name="rdesc" class="layui-textarea"></textarea>
            </div>
        </div>
        <div class="layui-form-item">
            <button type="submit">增加</button>
        </div>
    </form>
</div>
<script>

    var zTreeObj;
    // zTree 的参数配置，深入使用请参考 API 文档（setting 配置详解）
    var setting = {

        callback:{

            onCheck: function(obj){ //绑定修改的回调函数
                var nodes = zTreeObj.getCheckedNodes();

                //每一次修改,就改变我们隐藏域的值; 为了提交到后台去
                var vt = '';
                for(node in nodes){

                    if(node == nodes.length-1){
                        vt += nodes[node].auid;
                    }else{
                        vt += nodes[node].auid+",";
                    }
                }

                $("input[name='auths']").val(vt);
            }

        },

        check: {
            enable: true, //展示复选框
            //chkStyle:"checkbox",
            chkboxType: {"Y": "p", "N": "s"},  //设置复选框的父子关联
            check:"isChecked"
        },

        data: {
            key: {
                name: "auname" //树节点展示的内容
            },
            simpleData: {
                enable: true,
                idKey: "auid", //数据的主键
                pIdKey: "aupid" //数据的父id
            }
        }

    };

    $(document).ready(function () {

        $.ajax({ //向后台请求所有的权限信息;  data -> List<AuthPojo>
            url: "queryAuths",
            type: "post",
            success: function (data) {
                zTreeObj = $.fn.zTree.init($("#mytree"), setting, data);
            }
        });

    });

</script>
</body>

</html>